#!/usr/bin/env python3

import sys
import os
from stat import *
import date

#  0_class.txt:  0100000 | 0400 | 0200 | 040 | 04   0100644

#  文件类型:
    #  S_IFMT     0170000   文件类型的掩码
    #  S_IFSOCK   0140000   socket文件
    #  S_IFLNK    0120000   软链接文件
    #  S_IFREG    0100000   普通文件
    #  S_IFBLK    0060000   块设备文件
    #  S_IFDIR    0040000   目录
    #  S_IFCHR    0020000   字符设备文件
    #  S_IFIFO    0010000   管道文件

#  权限:
#  S_ISUID     04000   用户特权位
#  S_ISGID     02000   组特权位
#  S_ISVTX     01000   粘贴位

#  S_IRWXU     00700   所属用户所有权限
#  S_IRUSR     00400   所属用户读权限
#  S_IWUSR     00200   所属用户写权限
#  S_IXUSR     00100   所属用户的执行权限

#  S_IRWXG     00070   所属组所有权限
#  S_IRGRP     00040   所属组读权限
#  S_IWGRP     00020   所属组写权限
#  S_IXGRP     00010   所属组执行权限

#  S_IRWXO     00007   其它用户所有权限
#  S_IROTH     00004   其它用户读权限
#  S_IWOTH     00002   其它用户写权限
#  S_IXOTH     00001   其它用户执行权限


def fileType(mode):
    mode = mode if type(mode) == int else lstat(mode).st_mode
    return " pc d b - l s"[S_IFMT(mode) >> 12]

def filePerm(mode, permType='a'):
    mode = mode if type(mode) == int else lstat(mode).st_mode

    if permType == 'a':
        return filePerm(mode, 'u') + filePerm(mode, 'g') + filePerm(mode, 'o')

    perm = {'u': {'mask':S_IRWXU, 'offset':6, 'ismask':S_ISUID, 'ch':'s'},
            'g': {'mask':S_IRWXG, 'offset':3, 'ismask':S_ISGID, 'ch':'s'},
            'o': {'mask':S_IRWXO, 'offset':0, 'ismask':S_ISVTX, 'ch':'t'}}
    rwx = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]

    r = rwx[(mode & perm[permType]['mask']) >> perm[permType]['offset']]

    ch = perm[permType]['ch']
    if mode & perm[permType]['ismask']:
        r = r[0:2] + (ch if 'x' in r else ch.upper())

    return r

def getName(uid, filename='passwd'):
    for line in open('/etc/' + filename):
        m = line.split(':')
        if int(m[2]) == uid:
            return m[0]

    return uid

def getGroupName(gid):
    return getName(gid, 'group')


def main(argv):
    file = "." if len(argv) < 2 else argv[1]


    s = os.lstat(file)

    #  ftype = S_IFMT(s.st_mode)

    #  if ftype == S_IFSOCK:
        #  t = 's'
    #  elif ftype == S_IFLNK:
        #  t = 'l'
    #  elif ftype == S_IFBLK:
        #  t = 'b'
    #  elif ftype == S_IFIFO:
        #  t = 'p'
    #  elif ftype == S_IFDIR:
        #  t = 'd'
    #  elif ftype == S_IFCHR:
        #  t = 'c'
    #  else:
        #  t = '-'
    #  t += 'r' if S_IRUSR & s.st_mode else '-'
    #  t += 'w' if S_IWUSR & s.st_mode else '-'
    #  t += 'x' if S_IXUSR & s.st_mode else '-'

    #  t += 'r' if S_IRGRP & s.st_mode else '-'
    #  t += 'w' if S_IWGRP & s.st_mode else '-'
    #  t += 'x' if S_IXGRP & s.st_mode else '-'

    #  t += 'r' if S_IROTH & s.st_mode else '-'
    #  t += 'w' if S_IWOTH & s.st_mode else '-'
    #  t += 'x' if S_IXOTH & s.st_mode else '-'
    print(fileType(s.st_mode) + filePerm(s.st_mode),
          s.st_nlink, getName(s.st_uid), getGroupName(s.st_gid),
          s.st_size, date.ctime(s.st_mtime), file)

#  os.stat_result(
#  st_mode=33188,      文件权限及文件类型
#  st_ino=405425,      文件系统的结点号(inode)
#  st_dev=2055,        设备号
#  st_nlink=1,         硬链接数
#  st_uid=1000,        所属用户ID
#  st_gid=1000,        所属组ID
#  st_size=5401,       文件大小
#  st_atime=1506412874,    最后访问时间 cat
#  st_mtime=1506412786,    最后修改时间 vim
#  st_ctime=1506412786)    最后状态修改时间 chown/chmod


if __name__ == "__main__":
    main(sys.argv)
